package com.lushu.data {
import com.lushu.data.interfaces.ITreeProvider;
import com.lushu.info.ApplicationData;
import com.sfdk.events.IOEvent;
import com.sfdk.io.GenericIO;

import flash.display.BitmapData;
import flash.events.Event;
import flash.events.EventDispatcher;

import parts.renderer.ChargementBox;

public class TilesetImage extends EventDispatcher implements ITreeProvider {
    private var name_:String;
    private var ioUtil_:GenericIO;

    private var tilesetImage_:BitmapData;
    private var box_:ChargementBox;

    public function TilesetImage(tilesetName:String, box:ChargementBox) {
        box_ = box;
        name_ = tilesetName;
        if (name_.split(".").length == 1)
            ioUtil_ = new GenericIO(ApplicationData.BASE_LOCATION + "/" + ApplicationData.TILESET_DIRECTORY + name_ + ".png", GenericIO.BITMAP_FILE);
        else
            ioUtil_ = new GenericIO(ApplicationData.BASE_LOCATION + "/" + ApplicationData.TILESET_DIRECTORY + name_, GenericIO.BITMAP_FILE);
        ioUtil_.addEventListener(IOEvent.LOAD_COMPLETE, parseTilesetData);
        //ioUtil_.read(GenericIO.BITMAP_FILE);
    }

    private function parseTilesetData(event:IOEvent):void {
        tilesetImage_ = ioUtil_.getImageData();
        box_.setSubLabel("Tileset : " + name_);
        dispatchEvent(new Event("tilesetLoaded"));
    }

    public function getData():BitmapData {
        return tilesetImage_;
    }

    public function getName():String {
        return name_;
    }

    public function get label():String {
        return name_;
    }
}
}